import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import axios from "axios";
import VueAxios from "vue-axios";
import VueCookie from "vue-cookie";
// import env from "./env";
//懒加载插件引入
import VueLazyLoad from "vue-lazyload";
import store from "./store/index.js";
import { Message } from "element-ui";
import "element-ui/lib/theme-chalk/index.css";

Vue.config.productionTip = false;

const mock = false;
if (mock) {
  require("./mock/api");
}

axios.defaults.baseURL = "/api";
// console.log(env.baseURL);
axios.defaults.timeout = 8000;
// axios.defaults.baseURL = env.baseURL;

// 添加请求拦截器
axios.interceptors.request.use(
  function(config) {
    // 在发送请求之前做些什么
    return config;
  },
  function(error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

// 添加响应拦截器
axios.interceptors.response.use(
  function(response) {
    // 对响应数据做点什么
    let res = response.data;
    if (res.status == 0) {
      return res.data;
    } else if (res.status == 10) {
      window.location.href = "/#/login";
      return Promise.reject(res);
    } else {
      Message.warning(res.msg);
      return Promise.reject(res);
    }
  },
  function(error) {
    // 对响应错误做点什么
    let res = error.response;
    Message.error(res.data.message);
    return Promise.reject(error);
  }
);

Vue.use(VueAxios, axios);
Vue.use(VueCookie);
Vue.use(VueLazyLoad, {
  loading: "/imgs/loading-svg/loading-bars.svg",
});

Vue.prototype.$message = Message;

new Vue({
  store,
  router,
  render: (h) => h(App),
}).$mount("#app");
